# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse

from HelloWordTestModel.My_forms import EmpForm


def childrenHello(request):
    return HttpResponse("业务-childrenHello")

def hello(request):
    return HttpResponse("业务-hello")

def requestParamData(request,year,month=18):
    return HttpResponse(str(year)+"---------"+str(month))

def requestParamDataOne(request,year):
    return HttpResponse(str(year))

from django.http import HttpResponseRedirect
from django.urls import reverse

def redirect_to_year(request):
    # ...
    year = 2019
    # ...
    return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))

from django.shortcuts import redirect

def index(request):
    year = 2019
    return redirect(reverse('news-year-archive',args=(year,)))

from HelloWordTestModel import models

#登录
def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    username = request.POST.get("username")
    password = request.POST.get("pwd")
    user_obj = models.UserInfo.objects.filter(username=username, password=password).first()
    print(user_obj.username)
    if not user_obj:
        return redirect("/testModel/login/")
    else:
        rep = redirect("/testModel/index/")
        rep.set_cookie("is_login", True)
        return rep

#注册
def register(request):
    if request.method == "GET":
        return render(request, "register.html")
    username = request.POST.get("username")
    password = request.POST.get("pwd")
    user_obj = models.UserInfo.objects.filter(username=username, password=password).first()
    if not user_obj:
        userInfo = models.UserInfo(username=username, password=password)
        userInfo.save()
        return HttpResponse("注册成功")
    else:
        rep = redirect("/testModel/login/")
        return rep


def index(request):
    print(request.COOKIES.get('is_login'))
    status = request.COOKIES.get('is_login')  # 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的 cookie
    if not status:
        return redirect('/testModel/login/')
    return render(request, "index.html")


def logout(request):
    rep = redirect('/testModel/login/')
    rep.delete_cookie("is_login")
    return rep  # 点击注销后执行,删除cookie,不再保存用户状态，并弹到登录页面


def order(request):
    print(request.COOKIES.get('is_login'))
    status = request.COOKIES.get('is_login')
    if not status:
        return redirect('/testModel/login/')
    return render(request, "order.html")



def add_emp(request):
    if request.method == "GET":
        form = EmpForm()  # 初始化form对象
        return render(request, "add_emp.html", {"form":form})
    else:
        form = EmpForm(request.POST)  # 将数据传给form对象
        if form.is_valid():  # 进行校验
            data = form.cleaned_data
            data.pop("r_salary")
            models.Emp.objects.create(**data)
            return redirect("/testModel/index/")
        else:  # 校验失败
            clear_errors = form.errors.get("__all__")  # 获取全局钩子错误信息
            return render(request, "add_emp.html", {"form": form, "clear_errors": clear_errors})

